package com.goods.controller.business;


import com.goods.common.model.business.Product;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/business/product")
public class ProductController {

    @Autowired
    ProductService productService;


    //物资资料分页列表展示
    @GetMapping("/findProductList")
    public ResponseBean<Map<Object,Object>> findProductList(
            Integer pageNum,Integer pageSize,String name,String categorys,String supplier,Integer status
    ){
        Map map = new HashMap();
        List<Product> productList = productService.findProductList(name,categorys,supplier,status);
        List<Product> list = ListPageUtils.page(productList, pageSize, pageNum);
        map.put("total",productList.size());
        map.put("rows",list);
        return ResponseBean.success(map);
    }


    //添加物资
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductVO productVO){
        int i = productService.add(productVO);
        if (i == 0){
            return ResponseBean.error("添加失败");
        }else {
            return ResponseBean.success();
        }
    }

    //编辑物资->数据回显
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        Product product = productService.edit(id);
        return ResponseBean.success(product);
    }

    //编辑物资->更新保存
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,@RequestBody ProductVO productVO){
        int i = productService.update(id,productVO);
        if (i == 0){
            return ResponseBean.error("更新失败");
        }else {
            return ResponseBean.success();
        }
    }

    //物资回收站
    @PutMapping("/remove/{id}")
    public ResponseBean remove(@PathVariable Long id){
        int i = productService.remove(id);
        if (i == 0){
            return ResponseBean.error("移入回收站失败");
        }else {
            return ResponseBean.success();
        }
    }

    //物资回收站
    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id){
        int i = productService.back(id);
        if (i == 0){
            return ResponseBean.error("回收站恢复失败");
        }else {
            return ResponseBean.success();
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        int i = productService.delete(id);
        if (i == 0){
            return ResponseBean.error("回收站删除失败");
        }else {
            return ResponseBean.success();
        }
    }

    //物资审核
    @PutMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        int i = productService.publish(id);
        if (i == 0){
            return ResponseBean.error("审核失败");
        }else {
            return ResponseBean.success();
        }
    }

    @GetMapping("/findProducts")
    public ResponseBean<Map<Object,Object>> findProducts(Integer pageNum,Integer pageSize,Integer status){
        List<Product> productList = productService.findProducts(status);
        List<Product> page = ListPageUtils.page(productList, pageSize, pageNum);
        Map map = new HashMap();
        map.put("total",productList.size());
        map.put("rows",page);
        return ResponseBean.success(map);
    }

    @GetMapping("/findAllStocks")
    public ResponseBean findAllStocks(Integer pageSize,Integer pageNum,String categorys,String name){
        List<ProductStockVO> stockVOS = productService.findProductStocks(categorys,name);
        return ResponseBean.success(stockVOS);
    }

    @GetMapping("/findProductStocks")
    public ResponseBean findProductStocks(Integer pageSize,Integer pageNum,String categorys,String name){
        Map map = new HashMap();
        List<ProductStockVO> stocks = productService.findProductStocks(categorys,name);
        List<ProductStockVO> page = ListPageUtils.page(stocks, pageSize, pageNum);
        map.put("total",stocks.size());
        map.put("rows",page);
        return ResponseBean.success(map);
    }


}
